define(["angularAMD"], function (angularAMD) {
    angularAMD.service('QT05ModelSettingService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {

            this.init = function(option,call_back){
                openDialog(option,call_back)
            };

            function openDialog(option,call_back){
                $mdDialog.dialog(
                    'module/moduleQT/program/QT05/QT05ModelSetting.html',
                    function (dialog) {
                        return {
                            css_url:'module/moduleQT/program/QT05/QT05.css',
                            model_name: option.model_name,
                            panel_list: angular.copy(option.panel_list),
                            beforeShown:function(){
                                var checklist_position = '.QT05ModelSetting .kmi-layout-page .ps-scrollbar-content';

                                sortablejs.create($(checklist_position)[0], {
                                    ghostClass: 'ghost',
                                    filter: '.undragable',
                                    onEnd: function (evt) {
                                        var newIndex = evt.newIndex;
                                        var oldIndex = evt.oldIndex;
                                        var chooseItem = dialog.panel_list.splice(oldIndex, 1)[0];
                                        dialog.panel_list.splice(newIndex, 0, chooseItem);
                                        dialog.panel_list.forEach(function(item, index){
                                            item.seq = index;
                                        });
                                    }
                                });
                            },
                            init:function(){
                                for(var i in dialog.panel_list)
                                {
                                    dialog.panel_list[i].show_up = true;
                                    dialog.panel_list[i].show_down = true;
                                    // dialog.panel_list[i].seq = i;
                                    if(i == 0)
                                        dialog.panel_list[i].show_up = false;
                                    if(i == (dialog.panel_list.length - 1))
                                        dialog.panel_list[i].show_down = false;
                                }
                            },
                            panel_item_change:function(panel_item){
                                var has_visible = $filter('filter')(dialog.panel_list,{visible:true});
                                if(has_visible.length == 8 && panel_item.visible == false)
                                    return;
                                if(has_visible.length ==  1 && panel_item.panel_code == has_visible[0].panel_code)
                                    return;
                                if(panel_item.visible)
                                    panel_item.visible  = false;
                                else
                                    panel_item.visible  = true;
                            },
                            panel_item_up:function(panel_item,index){
                                var temp_array = [];
                                var move_index = index-1;
                                if(move_index >= 0 )
                                {
                                    for(var i in dialog.panel_list)
                                    {
                                        var item = dialog.panel_list[i];
                                        if(temp_array.length == move_index)
                                            temp_array.push(panel_item);
                                        if(i != index)
                                            temp_array.push(item);
                                    }
                                }

                                if(temp_array.length >0)
                                {
                                    dialog.panel_list = temp_array;
                                    for(var i in dialog.panel_list)
                                    {
                                        dialog.panel_list[i].seq = i;
                                        dialog.panel_list[i].show_up = true;
                                        dialog.panel_list[i].show_down = true;
                                        if(i == 0)
                                            dialog.panel_list[i].show_up = false;
                                        if(i == (dialog.panel_list.length - 1))
                                            dialog.panel_list[i].show_down = false;
                                    }
                                }
                            },
                            panel_item_down:function(panel_item,index){
                                var temp_array = [];
                                var move_index = index+1;
                                if(move_index < dialog.panel_list.length)
                                {
                                    for(var i in dialog.panel_list)
                                    {
                                        var item = dialog.panel_list[i];
                                        if(temp_array.length == move_index)
                                            temp_array.push(panel_item);
                                        if(i != index)
                                            temp_array.push(item);   
                                    }
                                }
                                if(temp_array.length >0)
                                {
                                    dialog.panel_list = temp_array;
                                    for(var i in dialog.panel_list)
                                    {
                                        dialog.panel_list[i].seq = i;
                                        dialog.panel_list[i].show_up = true;
                                        dialog.panel_list[i].show_down = true;
                                        if(i == 0)
                                            dialog.panel_list[i].show_up = false;
                                        if(i == (dialog.panel_list.length - 1))
                                            dialog.panel_list[i].show_down = false;
                                    }
                                }  
                            },
                            reduction:function(){
                                dialog.panel_list = angular.copy(option.panel_list);
                                for(var i in dialog.panel_list)
                                {
                                    dialog.panel_list[i].show_up = true;
                                    dialog.panel_list[i].show_down = true;
                                    dialog.panel_list[i].seq = i;
                                    if(i == 0)
                                        dialog.panel_list[i].show_up = false;
                                    if(i == (dialog.panel_list.length - 1))
                                        dialog.panel_list[i].show_down = false;
                                }
                            },
                            back:function(){
                                dialog.hide();
                            },
                            confirm: function(){
                                dialog.hide();
                                for(var i in dialog.panel_list)
                                {
                                    dialog.panel_list[i].active = false;
                                }
                                var firstPanel = $filter('filter')(dialog.panel_list, {visible: true})[0];
                                firstPanel.active = 1
                                // 待後端服務修正後開啟

                                // if (dialog.model_name) {
                                //     var content = {
                                //         request_type: 1,
                                //         user_template: [
                                //             {
                                //                 model_name: '',
                                //                 mode_sheets: []
                                //             }
                                //         ]
                                //     };
                                //     content.user_template[0].mode_sheets = dialog.panel_list.filter((sheet) => sheet.visible).map((sheet) => ({
                                //         mode_sheet: sheet.panel_code,
                                //         seq: sheet.seq
                                //     }));
                                //     userTemplateProcess(content)
                                // }else {
                                //     call_back(dialog.panel_list);
                                // }
                                call_back(dialog.panel_list);
                                
                            },
                            getPanelTitle: function(titleName) {
                                return titleName + $filter('translate')('QT05.details')
                            }
                        };
                    }
                );
            };    

            // 模板更新服務: 新增，檢查，調整排序
            function userTemplateProcess(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.user_template_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };
        }
    ])
});
